/**
 * 
 */
package com.zc.BankCreditManagement.ZCUtil;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author ZhangCheng
 * @time 2019年9月26日
 */
public class SendEmail {
	Properties properties;
	// 得到回话对象
	Session session;
	// 获取邮件对象
	Message message;
	// 得到邮差对象
	Transport transport;

	public SendEmail() {
		this.properties = new Properties();
	}

	private void initProperties() {
		this.properties.put("mail.transport.protocol", "smtp");// 连接协议
		this.properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
		this.properties.put("mail.smtp.port", 465);// 端口号
		this.properties.put("mail.smtp.auth", "true");
		this.properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
		this.properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
		this.session = Session.getInstance(this.properties);
		this.message = new MimeMessage(this.session);
	}

	private boolean sendingEmail() {

		try {
			this.transport = session.getTransport();
			// 连接自己的邮箱账户
			this.transport.connect("6873094@qq.com", "kxklgznwvwftbigd");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
			// 发送邮件
			this.transport.sendMessage(message, message.getAllRecipients());
			this.transport.close();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			return false;
			
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
			
		}
		return true;
	}

	public String sendEmailContent(String from, String nickName,String title,String content,String... tos) {
		initProperties();
		if (tos.length == 0) {
			return "收件人为空";
		}

		try {
			String pattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
			// 设置发件人邮箱地址
			if(from.matches(pattern)) {
				message.setFrom(new InternetAddress(from, nickName, "UTF-8"));
			}else {
				return "发件人邮箱格式不正确";
			}
			
			if (tos.length == 1) {// 单个收件人
				if (!tos[0].matches(pattern)) {
					return "1号收件人邮箱格式错误";
				}
				message.setRecipient(Message.RecipientType.TO, new InternetAddress(tos[0]));
			} else {// 多个收件人
				InternetAddress[] iA = new InternetAddress[tos.length];
				for (int i = 0; i < tos.length; i++) {
					if (!tos[i].matches(pattern)) {
						return (i + 1) + "号收件人邮箱格式错误";
					}
					iA[i] = new InternetAddress(tos[i]);// 添加到数组
				}
				message.setRecipients(Message.RecipientType.TO, iA);
			}
			// 设置收件人邮箱地址

//			 message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
			// 设置邮件标题
			message.setSubject(title);
			// 设置邮件内容
//			        message.setText("Here IS MY JAVA FIRST EMAIL");
			message.setContent(content, "text/html;charset=UTF-8");
			message.setSentDate(new Date());
		} // 一个收件人
		catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "字符编码错误！";
		} 
		catch (MessagingException e) {
			// TODO Auto-generated catch block
			
			return e.getMessage();
		}
		if(sendingEmail()) {
			return "Yes 您的验证码已发送成功，请注意查收！";
		}else {
			return "No 发送失败！请稍后重试！";
		}
		

	}

//	public static void main(String[] args) throws AddressException, MessagingException, UnsupportedEncodingException {
//		Properties properties = new Properties();
//		properties.put("mail.transport.protocol", "smtp");// 连接协议
//		properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
//		properties.put("mail.smtp.port", 465);// 端口号
//		properties.put("mail.smtp.auth", "true");
//		properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
//		properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
//		// 得到回话对象
//		Session session = Session.getInstance(properties);
//		// 获取邮件对象
//		Message message = new MimeMessage(session);
//		// 设置发件人邮箱地址
//
//		message.setFrom(new InternetAddress("6873094@qq.com", "您的张", "UTF-8"));
//
//		// 设置收件人邮箱地址
////		        message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
//		message.setRecipient(Message.RecipientType.TO, new InternetAddress("2268874211@qq.com"));// 一个收件人
//		// 设置邮件标题
//		message.setSubject("老胡，好久不见");
//		// 设置邮件内容
////		        message.setText("Here IS MY JAVA FIRST EMAIL");
//		message.setContent("<h1>这里是Bro_Zhang</h1>", "text/html;charset=UTF-8");
//		// 得到邮差对象
//		Transport transport = session.getTransport();
//		// 连接自己的邮箱账户
//		transport.connect("6873094@qq.com", "kxklgznwvwftbigd");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
//		// 发送邮件
//		transport.sendMessage(message, message.getAllRecipients());
//		transport.close();
//	}
}
